import GameDB, { Poker } from "../GameDB";
import UIPoker from "../../View/UIPoker/UIPoker";
import { EPokerStatus } from "../ConfigEnum";
import { UIUtil } from "../../../GameFramework/Util/UIUtil";
import View from "../../../GameFramework/MVC/View";
import GameEvent from "../GameEvent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameView extends View {
    @property(cc.Prefab) pokerPrefab: cc.Prefab = null;
    @property(cc.Node) initPokerArea: cc.Node = null;
    @property(cc.Node) closeSendArea: cc.Node = null;
    @property(cc.Node) openSendArea: cc.Node = null;
    @property([cc.Node]) receiveAreaList: cc.Node[] = [];

    @property(cc.Node) PlayGroundRoot: cc.Node = null;
    // private playGroundList: cc.Node[] = [];

    private m_Model: GameDB = null;
    public onLoad() {
        // for (let i = 0; i <= GameDB.CONST_PLAY_GROUPS; ++i) {
        //     let playGroup = new cc.Node();
        //     playGroup.position = new cc.Vec3(85 * i, 0, 0)
        //     this.PlayGroundAnchor.addChild(playGroup)
        //     this.playGroundList.push(playGroup)

        // }

    }

    public BindModel(model: GameDB) {
        this.m_Model = model
        this.m_Model.on(GameEvent.INIT_POKER, this.onEventInit, this);
        this.m_Model.on(GameEvent.PLAY, this.onEventPlay, this);
        this.m_Model.on(GameEvent.INIT_GROUP_CARD, this.onEventInitGroupCard, this);
        this.on(GameEvent.CS_POKER_MOVE_FROM_PLAYAREA_TORECEIVE, this.m_Model.OnEventPokerMoveFromPlayAreaToReceiveArea, this.m_Model);

    }
    public UnBindModel() {
        this.m_Model.off(GameEvent.INIT_POKER, this.onEventInit);
        this.m_Model.off(GameEvent.PLAY, this.onEventPlay);
        this.m_Model.off(GameEvent.INIT_GROUP_CARD, this.onEventInitGroupCard);
        this.off(GameEvent.CS_POKER_MOVE_FROM_PLAYAREA_TORECEIVE, this.m_Model.OnEventPokerMoveFromPlayAreaToReceiveArea);
        this.m_Model = null;

    }
    public InitPokers(pokers: Poker[]) {
        //创建所有扑克牌UI
        pokers.forEach((poker, index) => {
            let uiPoker = this.CreateUIPoker(poker)
            uiPoker.node.x = 0.2 * index;
            uiPoker.node.y = 0.2 * index;
            this.initPokerArea.addChild(uiPoker.node)
        });
    }
    private OnPlay() {
        //牌的顺序
        let stack: cc.Node[] = [];
        for (let i = this.initPokerArea.children.length - 1; i >= 0; --i) {
            let child = this.initPokerArea.children[i];
            stack.push(child)
            this.initPokerArea.removeChild(child)

        }
        for (let i = stack.length - 1; i >= 0; --i) {
            let child = stack[i];
            this.closeSendArea.addChild(child)
        }
    }
    public onEventInit(pokers) {
        this.InitPokers(pokers);
    }

    public onEventPlay() {
        this.OnPlay();
    }
    public onEventInitGroupCard(groupIndex: number, cardIndex: number, poker: Poker) {
        let index = GameDB.CONST_PLAY_GROUPS * cardIndex - cardIndex * (cardIndex - 1) / 2 - cardIndex + groupIndex
        //先移动UI
        let node = poker.view.node
        UIUtil.move(node, this.PlayGroundRoot, false);
        node.zIndex = index;

        let delay = index * 0.05;
        let px = groupIndex * 85;
        if (poker.status == EPokerStatus.OPEN) {
            cc.tween(node)
                .delay(delay)
                .to(0.5, { position: cc.v2(px, -30 * cardIndex) })
                .to(0.3, { scaleX: 0 })
                .call(() => {
                    //UI的显示状态刷新过来
                    poker.view.Refresh()
                })
                .to(0.3, { scaleX: 1 })
                .start()
        } else {
            cc.tween(node)
                .delay(delay)
                .to(0.5, { position: cc.v2(px, -30 * cardIndex) })
                .start()
        }
    }
    // -------------private API--------------
    private CreateUIPoker(poker: Poker): UIPoker {
        let uiPokerNode = cc.instantiate(this.pokerPrefab)
        let uiPoker: UIPoker = uiPokerNode.getComponent(UIPoker)
        uiPoker.Init(poker, this)

        return uiPoker
    }

    private isLocationPlayArea(uiPoker: UIPoker): boolean {
        return this.m_Model.isLocationPlayArea(uiPoker.poker);
    }

    private isPlayAreaIndexTop(uiPoker: UIPoker): boolean {
        return this.m_Model.isPlayAreaIndexTop(uiPoker.poker);
    }
    //--------------public api---------------

    public OnClickUIPoker(uiPoker: UIPoker) {
        //todo
        //1、这张牌如果在玩牌区
        //2、这张牌时翻开的
        //3、这张牌是最上方的一张牌
        //4、这张牌的点数是A
        //-->这张牌可以一移动到收牌区
        if (this.isLocationPlayArea(uiPoker)) {
            if (uiPoker.isOpen()) {
                if (this.isPlayAreaIndexTop(uiPoker)) {
                    if (uiPoker.isPoint(1)) {
                        this.emit(GameEvent.CS_POKER_MOVE_FROM_PLAYAREA_TORECEIVE, uiPoker.poker);
                    }
                }
            }
        } else {

        }

    }
}